﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <title>Posicionamiento de la cuenta account en Bing Maps</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

		<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
		<script type="text/javascript" src="script/<Helper SDK CRM>.js"></script>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

      <script type="text/javascript">
         
         var map = null;
 
         function GetMap()
         {
            map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),{
				credentials:"AlNlWmcd6TQlGcWllbomTNLr5BEAz9nlM5TX2YQ0VRGPhXekVvEKxmP7xMU53GbA", 
				mapTypeId:Microsoft.Maps.MapTypeId.road
				}); 
			ClickGeocode();
         }

         function ClickGeocode(credentials)
         {
            map.getCredentials(MakeGeocodeRequest);
         }

         function MakeGeocodeRequest(credentials)
         {			
			var Primary = window.parent.Xrm.Page.getAttribute("new_address1_tipovia").getValue() + " " +
							window.parent.Xrm.Page.getAttribute("address1_line1").getValue() + " " +
							window.parent.Xrm.Page.getAttribute("address1_line2").getValue() + " " +
							window.parent.Xrm.Page.getAttribute("address1_postalcode").getValue() + " " +
							window.parent.Xrm.Page.getAttribute("address1_stateorprovince").getValue();
		 
			var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations?query=" + encodeURI(Primary) + "&output=json&jsonp=GeocodeCallback&key=" + credentials;
			CallRestService(geocodeRequest);
		 
			var Id = window.parent.Xrm.Page.data.entity.getId(); //$.getUrlVar('oId');				
			var ar_Direccion = SDK.JQuery.retrieveMultipleRecords(
				"Account",
				"*(guid'"+ Id +"')/Referencedaccount_parent_account"
				);
 
			if (ar_Direccion && ar_Direccion.results) {
				for (var i = 0; i < ar_Direccion.results.length; i++)
				{
					//alert(ar_Direccion.results[i].Name);
					var Complete = ar_Direccion.results[i].new_address1_tipovia +" " +
									ar_Direccion.results[i].Address1_Line1 +" " +
									ar_Direccion.results[i].Address1_Line2 + " " +
									ar_Direccion.results[i].Address1_PostalCode + " " +
									ar_Direccion.results[i].Address1_StateOrProvince;
					
					geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations?query=" + encodeURI(Complete) + "&output=json&jsonp=GeocodeCallback&key=" + credentials;
					CallRestService(geocodeRequest);
				}
			}   
            
			
            
         }

         function GeocodeCallback(result) 
         {
            //alert("Found location: " + result.resourceSets[0].resources[0].name);

            if (result &&
                   result.resourceSets &&
                   result.resourceSets.length > 0 &&
                   result.resourceSets[0].resources &&
                   result.resourceSets[0].resources.length > 0) 
            {
               // Set the map view using the returned bounding box
               var bbox = result.resourceSets[0].resources[0].bbox;
               var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
               map.setView({ bounds: viewBoundaries});

               // Add a pushpin at the found location
               var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
               var pushpin = new Microsoft.Maps.Pushpin(location);
               map.entities.push(pushpin);
            }
         }

         function CallRestService(request) 
         {
            var script = document.createElement("script");
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", request);
            document.body.appendChild(script);
         }
      </script>
   </head>
   <body onload="GetMap();">
      <div id='mapDiv' style="position:relative; width:100%; height:300px;"></div>
      <!--
	  <input id="txtQuery" type="text" value="Portland"/>
      <input type="button" value="Geocode" onclick="ClickGeocode()"/>       
	  -->
   </body>
</html>